using System.Windows;
using System.Windows.Input;

namespace WPFTemplateLib.Attached.AttachGroup
{
	public class ScrollElement
	{
		#region [附加属性] 是否重新引发鼠标滚轮事件
		public static bool GetIsReRaiseMouseWheelEvent(DependencyObject obj)
		{
			return (bool)obj.GetValue(IsReRaiseMouseWheelEventProperty);
		}
		/// <summary>
		/// 设置 是否重新引发鼠标滚轮事件
		/// </summary>
		public static void SetIsReRaiseMouseWheelEvent(DependencyObject obj, bool value)
		{
			obj.SetValue(IsReRaiseMouseWheelEventProperty, value);
		}
		/// <summary>
		/// [附加属性] 是否重新引发鼠标滚轮事件
		/// </summary>
		public static readonly DependencyProperty IsReRaiseMouseWheelEventProperty =
			DependencyProperty.RegisterAttached("IsReRaiseMouseWheelEvent", typeof(bool), typeof(ScrollElement), new PropertyMetadata(false, IsReRaiseMouseWheelEventChangedCallback));
		/// <summary>
		/// 是否重新引发鼠标滚轮事件 变化处理方法
		/// </summary>
		private static void IsReRaiseMouseWheelEventChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			var element = d as FrameworkElement;
			if (element == null)
				return;

			if(e.NewValue is true)
			{
				element.PreviewMouseWheel += Element_PreviewMouseWheel;
			}
			else
			{
				element.PreviewMouseWheel -= Element_PreviewMouseWheel;
			}
		}

		/// <summary>
		/// 鼠标滚轮事件方法
		/// 参考：https://www.cnblogs.com/kevinWu7/p/10163546.html
		/// </summary>
		private static void Element_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
		{
			var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
			{
				RoutedEvent = UIElement.MouseWheelEvent, Source = sender
			};

			(sender as FrameworkElement)?.RaiseEvent(eventArg);
		}
		#endregion
	}
}
